package com.lee.springcloud.controller;

import com.lee.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Lee
 * @createDate 2022/1/5 11:48
 */
@Controller
/**
 * defaultFallback  全局fallback方法名
 * commandProperties 设置fallback方法参数，这里意为该方法响应时间超过500毫秒测调用payment_Global_FallbackMethod方法
 */
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod",commandProperties = @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "15000"))
public class OrderHystrixController {
    @Autowired
    private PaymentHystrixService paymentHystrixService;

    @GetMapping(value = "/consumer/payment/hystrix/ok/{id}")
    @ResponseBody
    public String paymentInfo_OK(@PathVariable("id") Integer id){
        //int a = 10/0;
        return paymentHystrixService.paymentInfo_OK(id);
    }


    @GetMapping(value = "/consumer/payment/hystrix/timeout/{id}")
    @ResponseBody
    /*  精确fallback方法
    @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000")
    })
    */
    @HystrixCommand
    public String paymentInfo_Timeout(@PathVariable("id") Integer id){
        //int a = 10/0;
        return paymentHystrixService.paymentInfo_Timeout(id);
    }

    /*  fallback方法
    public String paymentInfo_TimeOutHandler(Integer id) {
        return "/(ToT)/调用支付接口超时或异常:\t" + "\t当前线程池名字"+Thread.currentThread().getName();
    }
    */

    /** 全局fallback方法 **/
    public String payment_Global_FallbackMethod() {
        return "Global异常处理信息,请稍后再试，l(ToT)/~~";
    }

}
